package com.hengjun.baccarat.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class PostDataHolder {
	private static final String CHARSET_ENCODE = "utf-8";

	public String getDataFromServer(String serverUrl) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(serverUrl);
		String rtnString = "";
		try {
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, CHARSET_ENCODE);
			@SuppressWarnings("unused")
			int status = client.executeMethod(post);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					post.getResponseBodyAsStream(), "ISO-8859-1"));

			String tmp = null;
			String htmlRet = "";

			while ((tmp = reader.readLine()) != null) {
				htmlRet += tmp + "\r\n";
			}
			rtnString = new String(htmlRet.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			rtnString = null;
		} finally {
			post.releaseConnection();

		}
		return rtnString;
	}

	public String getDataFromServer(String serverUrl, NameValuePair[] pairs)
			throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(serverUrl);
		post.addParameters(pairs);
		String rtnString = "";
		try {
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, CHARSET_ENCODE);
			@SuppressWarnings("unused")
			int status = client.executeMethod(post);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					post.getResponseBodyAsStream(), "ISO-8859-1"));

			String tmp = null;
			String htmlRet = "";

			while ((tmp = reader.readLine()) != null) {
				htmlRet += tmp + "\r\n";
			}
			rtnString = new String(htmlRet.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			post.releaseConnection();
			rtnString = null;
			throw e;
		}

		return rtnString;
	}

	public String postDataWithAttach(String serverUrl, Part[] part) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(serverUrl);
		MultipartRequestEntity mrp = new MultipartRequestEntity(part,
				post.getParams());
		post.setRequestEntity(mrp);
		String rtnString = "";
		try {
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, CHARSET_ENCODE);
			@SuppressWarnings("unused")
			int status = client.executeMethod(post);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					post.getResponseBodyAsStream(), "ISO-8859-1"));

			String tmp = null;
			String htmlRet = "";

			while ((tmp = reader.readLine()) != null) {
				htmlRet += tmp + "\r\n";
			}
			rtnString = new String(htmlRet.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			rtnString = null;
		} finally {
			post.releaseConnection();

		}
		return rtnString;
	}

	public String getDataFromUpdateServer(String serverUrl,
			NameValuePair[] pairs) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(serverUrl);
		post.addParameters(pairs);
		client.getHttpConnectionManager().getParams()
				.setConnectionTimeout(3000);
		String rtnString = "";
		BufferedReader reader = null;
		try {
			post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
					new DefaultHttpMethodRetryHandler());// /不成功則重試三次
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, CHARSET_ENCODE);
			int status = client.executeMethod(post);
			if (status != 200) {
				throw new Exception("連接服務器地址失敗");
			}
			reader = new BufferedReader(new InputStreamReader(
					post.getResponseBodyAsStream(), "ISO-8859-1"));

			String tmp = null;
			String htmlRet = "";

			while ((tmp = reader.readLine()) != null) {
				htmlRet += tmp + "\r\n";
			}
			rtnString = new String(htmlRet.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception e) {
			rtnString = null;
		} finally {
			post.releaseConnection();
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
		return rtnString;
	}
}
